﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace CDSysLDManageProject
{
    public partial class FmCheckAdd : Form
    {

        public FmCheckAdd()
        {
            InitializeComponent();
        }

        static SocketTest st1 = new SocketTest();
        //static SocketTest st2 = new SocketTest();
        string ip1 = System.Configuration.ConfigurationManager.AppSettings["WCSAddress"];
        int port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["WCSPort"]);
        int port1 = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["WCSPort1"]);
        int port2 = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["WCSPort2"]);

        private void FmCheckAdd_Load(object sender, EventArgs e)
        {
            st1.CreateClient(ip1,port2);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            string add = tbxvalue.Text;
            string value = ReadRightValue(add, 1, "notnull");
            tbxvalue.Text = value;
        }

        //校验并发送读指令
        public string ReadRightValue(string eqadr, int eqno, string expectvalue)
        {
            int ngcount = 1;
            string recvalue = "";

            if (expectvalue != "notnull")
            {
                while (ngcount <= 3 && recvalue != expectvalue)
                {
                    if (st1.Connected() == false)
                    {
                        msgwarn.Text = "网络连接断开";
                        msgwarn.BackColor = Color.Red;
                        //WriteLogTxt(DateTime.Now + " " + "网络连接断开");
                        while (st1.Connected() == false)
                        {
                            ReConnect();
                        }

                    }
                    else if (st1.Connected() == true)
                    {
                        bool rdflag = st1.sendRddata(eqadr, eqno);
                        if (!rdflag)
                        {
                            //WriteLogTxt(DateTime.Now + "  读取" + eqadr + " 寄存器失败");
                        }

                        Thread.Sleep(100);
                        recvalue = st1.receivedata(eqno, "r");
                        ngcount++;

                    }


                }
            }
            else
            {
                while (ngcount <= 3 && recvalue == "")
                {
                    if (st1.Connected() == false)
                    {
                        msgwarn.Text = "网络连接断开";
                        msgwarn.BackColor = Color.Red;
                        //WriteLogTxt(DateTime.Now + " " + "网络连接断开");
                        while (st1.Connected() == false)
                        {
                            ReConnect();
                        }
                    }
                    else if (st1.Connected())
                    {
                        bool rdflag = st1.sendRddata(eqadr, eqno);
                        if (!rdflag)
                        {
                            //WriteLogTxt(DateTime.Now + "  读取" + eqadr + " 寄存器失败");
                        }
                        Thread.Sleep(200);
                        recvalue = st1.receivedata(eqno, "r");
                        ngcount++;
                    }

                }
            }

            Thread.Sleep(200);
            return recvalue;

        }

        //连接网络
        private void ReConnect()
        {
            if (!st1.Connected())
            {
                try
                {
                    st1.CreateClient(ip1, port1);
                }
                catch (Exception)
                {


                }

            }
            else
            {

                msgwarn.Text = "网络连接恢复";
                msgwarn.BackColor = Color.SpringGreen;
                return;
            }
        }



    }
}
